package com.quickstart.topic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quickstart.common.mybatis.page.PageForm;
import com.quickstart.entity.TopicEntity;
import com.quickstart.topic.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:wesley
 */
@Controller
@RequestMapping(value = "/topic")
public class TopicController {

    @Autowired
    private TopicService topicService;


    @RequestMapping(value = "/page")
    @ResponseBody
    public Map<String,Object> page(PageForm pageForm){
        PageHelper.startPage(pageForm.getPage(),pageForm.getLimit());
        List<TopicEntity> list = topicService.getAllTopic();
        PageInfo<TopicEntity> pageInfo = new PageInfo<>(list);
        Map<String,Object> map = new HashMap<>();
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("code","");
        map.put("msg","");
        return map;
    }



    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String,Object> save(TopicEntity topicEntity) {
        Map<String,Object> map = new HashMap<>();
        try {
            topicService.save(topicEntity);
            map.put("tag",true);
        } catch (Exception e){
            e.printStackTrace();
            map.put("tag",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }





}
